#include "comm.h"

// 读取
int main()
{
    umask(0);
    // server创建好了，client就不用创建了
    if (mkfifo(IPC_PATH, 0600) != 0)
    {
        cerr << "mkfifo error" << endl;
        return 1;
    }

    int pipeFd = open(IPC_PATH, O_RDONLY);
    if (pipeFd < 0)
    {
        cerr << "open fifo error" << endl;
        return 2;
    }
#define NUM 1024
    // 正常的通信过程
    char buffer[NUM];
    while (true)
    {
        ssize_t s = read(pipeFd, buffer, sizeof(buffer) - 1);
        if (s > 0)
        {
            buffer[s] = '\0';
            cout << "客户端->服务器#" << buffer << endl;
        }
        else if (s == 0)
        {
            cout << "客户退出了，我也推出了" << endl;
            break;
        }
        else
        {
            // do nothing
            cout << "read: " << strerror(errno) << endl;
        }
    }
    close(pipeFd);
    cout << "服务端退出了" << endl;

    // 跑完之后删除管道
    unlink(IPC_PATH);

    return 0;
}
